using System;
using System.Collections.Generic;
using System.Linq;
using Kotonoha.Objects.Model;
using Kotonoha.Server.Objects.Model;


namespace Kotonoha.Objects.Selectors {
    public class SimpleWordSelector : AbstractOneTimedSelector {
        public SimpleWordSelector(Container container) : base(container) {}

        private static readonly Random rand = new Random();

        protected override Word resolve() {
            List<Word> words = container.Words;
            List<Word> list = words
                .Where(w => w.LearningData != null)
                .Where(w => w.LearningData.IntervalEnd < DateTime.Now)
                .ToList();
            if (list.Count != 0) {
                list.Sort((w1,w2) => w1.LearningData.IntervalEnd.CompareTo(w2.LearningData.IntervalEnd));
                return list[0];
            }
            list = words.Where(w => w.LearningData == null).ToList();
            return list[rand.Next(list.Count)];
        }
    }
}